var craftsAllotPageVm  = new Vue({
    el: '#craftsAllotPage',
    data: {
        craftsAllotPageVisible: false,
        /* 供应商选择页面 */
        merchantsPageVisible: false,
        /* 外协商选择页面 */
        supplierPageVisible: false,
        /* 紫云外协商（自做时选择） */
        erpSupplierOffline: {},
        sellerId: '',
        orderNo: '',
        itemId: '',
        erpItemId: '',
        itemName: '',
        productId: '',
        totalAmount: '',
        allotAmount: 0,
        crafts: [],
        craftIdx: '',// 当前工艺索引
        factoryQuoteInfos: [],// 原报价工厂列表
        factoryQuoteInfosQuery: [],// 过滤后报价工厂列表
        q: {
            factoryName: '',
            factoryAddr: ''
        },
        /* 服务工序 */
        craftsOption: [{
            value: 'install',
            label: '安装'
        }, {
            value: 'pack',
            label: '打包'
        }],
        craftsOptionVal: [],
        /* 服务工序添加行 */
        serverCrafts: [],
        /* 服务工序列表索引 */
        serverCraftsIdx: [],
        /* 外协商分页参数 */
        supplierPage:{
            currPage:1, // 当前页
            pageSize:8, // 分页数
            totalCount:0, // 总条数
            tableData:[], //数据列
        },
        /* 外协商分页查询参数 */
        supplierQuery: {
            supplierName: '',
            contactName: ''
        },
        craftsAllotLoading: false,
    },
    methods: {
        /* 初始化 */
        loadData: function (data) {
            craftsAllotPageVm.sellerId = data.sellerId;
            craftsAllotPageVm.orderNo = data.orderNo;
            craftsAllotPageVm.totalAmount = data.totalAmount;
            craftsAllotPageVm.erpItemId = data.erpItemId;
            craftsAllotPageVm.itemId = data.itemId;
            craftsAllotPageVm.itemName = data.itemName;
            craftsAllotPageVm.productId = data.productId;
            $.get(baseURL + "order/erporder/orderCraftsInfo?itemId=" + data.itemId, function(r){
                if (r.code == 0){
                    craftsAllotPageVm.crafts = r.craftsList;
                    craftsAllotPageVm.serverCrafts = r.serverCraftsList;
                    craftsAllotPageVm.erpSupplierOffline = r.erpSupplierOffline;
                    craftsAllotPageVm.calcTotalPrice();
                    craftsAllotPageVm.craftsAllotPageVisible = true;
                }else{
                    alert(r.msg);
                }
            });
        },
        /* 打开供应商列表 */
        selectMerchants: function (row, idx) {
            craftsAllotPageVm.craftsAllotLoading = true;
            var postData = {
                orderItemId: craftsAllotPageVm.itemId,
                splitProcessType: row.splitProcessType,
                partId: row.partId,
                craftValueType: row.craftValueType,
                sellerId: craftsAllotPageVm.sellerId
            }
            $.ajax({
                type: "POST",
                url: baseURL + "order/erporder/orderCraftAllotFindFactory",
                contentType: "application/json",
                data: JSON.stringify(postData),
                success: function(r){
                    craftsAllotPageVm.craftsAllotLoading = false;
                    if(r.code == 0){
                        var factoryQuoteInfos = r.factoryQuoteInfos;
                        craftsAllotPageVm.factoryQuoteInfos = factoryQuoteInfos;
                        craftsAllotPageVm.factoryQuoteInfosQuery = factoryQuoteInfos;
                        console.info(craftsAllotPageVm.factoryQuoteInfosQuery);
                        craftsAllotPageVm.craftIdx = idx;
                        craftsAllotPageVm.merchantsPageVisible = true;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        /* 供应商列表查询 */
        queryMerchants: function () {
            var factoryName = craftsAllotPageVm.q.factoryName;
            var factoryAddr = craftsAllotPageVm.q.factoryAddr;
            if (isBlank(factoryName) && isBlank(factoryAddr)){
                craftsAllotPageVm.factoryQuoteInfosQuery = craftsAllotPageVm.factoryQuoteInfos;
            }else{
                craftsAllotPageVm.factoryQuoteInfosQuery = [];
                for (var f in craftsAllotPageVm.factoryQuoteInfos){
                    var queryNameHit = false;
                    var queryAddrHit = false;
                    var item = craftsAllotPageVm.factoryQuoteInfos[f];
                    if (!isBlank(factoryName)){
                        if (item.factoryInfoVO.factoryName.indexOf(factoryName) != -1){
                            queryNameHit = true;
                        }
                    }else {
                        queryNameHit = true;
                    }
                    if (!isBlank(factoryAddr)){
                        if (item.factoryInfoVO.address.indexOf(factoryAddr) != -1){
                            queryAddrHit = true;
                        }
                    }else{
                        queryAddrHit = true;
                    }
                    if (queryNameHit && queryAddrHit){
                        craftsAllotPageVm.factoryQuoteInfosQuery.push(item);
                    }
                }
            }
        },
        /* 选择供应商 */
        choiceMerchant: function (row) {
            var craft = craftsAllotPageVm.crafts[craftsAllotPageVm.craftIdx];
            Vue.set(craft, 'self', row.factoryInfoVO.self);
            Vue.set(craft, 'factoryId', row.factoryInfoVO.id);
            Vue.set(craft, 'factoryName', row.factoryInfoVO.factoryName);
            Vue.set(craft, 'address', row.factoryInfoVO.address);
            Vue.set(craft, 'contacts', row.factoryInfoVO.contacts);
            Vue.set(craft, 'contactWay', row.factoryInfoVO.contactWay);
            Vue.set(craft, 'totalPrice', row.totalPrice);
            Vue.set(craft, 'remark', '');
            craftsAllotPageVm.calcTotalPrice();
            craftsAllotPageVm.merchantsPageVisible = false;
        },
        closecraftsAllotPage: function () {},
        /* 打开外协商列表 */
        showSuppliers: function (idx) {
            craftsAllotPageVm.serverCraftsIdx = idx;
            craftsAllotPageVm.querySupplierPageList();
            craftsAllotPageVm.supplierPageVisible = true;
        },
        /* 查询外协商列表 */
        querySupplierPageList: function () {
            var queryData = {
                "limit":craftsAllotPageVm.supplierPage.pageSize,
                "page":craftsAllotPageVm.supplierPage.currPage,
                "supplierName": craftsAllotPageVm.supplierQuery.supplierName,
                "contactName": craftsAllotPageVm.supplierQuery.contactName,
                "state": "2",// 查询审核通过外协商列表
            }
            $.ajax({
                type: "GET",
                url: baseURL + "suppOffline/erpsupplieroffline/list",
                contentType: "application/json",
                data: queryData,
                success: function(r){
                    craftsAllotPageVm.supplierPage.tableData = r.page.list
                    craftsAllotPageVm.supplierPage.totalCount = r.page.totalCount
                }
            });
        },
        /*分页数改变*/
        handleSizeChange: function(val) {
            craftsAllotPageVm.supplierPage.pageSize = val;
            craftsAllotPageVm.querySupplierPageList();
        },
        /*分页当前页改变改变*/
        handleCurrentChange: function(val) {
            craftsAllotPageVm.supplierPage.currPage = val
            craftsAllotPageVm.querySupplierPageList();
        },
        /* 添加工序行 */
        addCraftsRow: function () {
            var craftsRow = {
                // 服务工序
                orderItemId: '',
                partId: '',
                partsName: '',
                craftValueType: '',
                attributeName: '',
                supplierId: '',
                supplierName: '',
                contactAddr: '',
                contactName: '',
                contactTel: '',
                totalPrice: '',
                remark: '',
                alloted: false,
                isOwn: false
            }
            craftsAllotPageVm.serverCrafts.push(craftsRow);
        },
        /* 选择外协商 */
        selectSupplier: function (data) {
            var serverCraft = craftsAllotPageVm.serverCrafts[craftsAllotPageVm.serverCraftsIdx];
            serverCraft.supplierId = data.supplierId;
            serverCraft.supplierName = data.supplierName;
            serverCraft.addr = data.contactAddr;
            serverCraft.contactName = data.contactName;
            serverCraft.contactTel = data.contactTel;
            craftsAllotPageVm.supplierPageVisible = false;
        },
        /* 服务工序，外协金额变化 */
        suppPriceChange: function (row) {
            craftsAllotPageVm.calcTotalPrice();
        },
        /* 工序自做勾选 */
        isOwnChange: function (idx, row) {
            var isOwn = row.isOwn;
            var craft = craftsAllotPageVm.crafts[idx];
            if (isOwn){
                if (!isBlank(craft.factoryId)){
                    this.$confirm('已选择供应商 '+ craft.factoryName+ '，是否确认自做工序？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (f) {
                            if (f == 'confirm'){
                                var supp = craftsAllotPageVm.erpSupplierOffline;
                                craft.factoryId = supp.supplierId;
                                craft.factoryName = supp.supplierName;
                                craft.address = supp.contactAddr;
                                craft.contacts = supp.contactName;
                                craft.contactWay = supp.contactTel;
                                craft.totalPrice = '';
                                craft.isOwn = true;
                            }else {
                                craft.isOwn = false;
                            }
                        }
                    })
                }else {
                    var supp = craftsAllotPageVm.erpSupplierOffline;
                    craft.factoryId = supp.supplierId;
                    craft.factoryName = supp.supplierName;
                    craft.address = supp.contactAddr;
                    craft.contacts = supp.contactName;
                    craft.contactWay = supp.contactTel;
                    craft.totalPrice = '';
                    craft.isOwn = true;
                }
            }else {
                craft.factoryId = '';
                craft.factoryName = '';
                craft.address = '';
                craft.contacts = '';
                craft.contactWay = '';
                craft.totalPrice = '';
                craft.isOwn = false;
            }
        },
        /* 分配总金额计算 */
        calcTotalPrice: function () {
            craftsAllotPageVm.allotAmount = 0;
            for (var c in craftsAllotPageVm.crafts){
                var totalPrice = craftsAllotPageVm.crafts[c].totalPrice;
                if (typeof totalPrice != 'undefined'){
                    craftsAllotPageVm.allotAmount += totalPrice;
                }
            }
            for (var s in craftsAllotPageVm.serverCrafts){
                var totalPrice = craftsAllotPageVm.serverCrafts[s].totalPrice;
                if (typeof totalPrice != 'undefined'){
                    craftsAllotPageVm.allotAmount += Number(totalPrice);
                }
            }
            if (craftsAllotPageVm.allotAmount != 0){
                craftsAllotPageVm.allotAmount = Math.floor(craftsAllotPageVm.allotAmount * 100) / 100;
            }
        },
        /* 下单 */
        generateOrder: function () {
            // 外协工序
            var serverCraftsList = [];
            for (var s in craftsAllotPageVm.serverCrafts){
                var craft = craftsAllotPageVm.serverCrafts[s];
                if (!craft.alloted){
                    serverCraftsList.push(craft);
                }
            }
            // 分配工厂工序
            var allotCrafts = [];
            for (var c in craftsAllotPageVm.crafts){
                var craft = craftsAllotPageVm.crafts[c];
                var factoryId = craft.factoryId;
                var alloted = craft.alloted;
                var isOwn = craft.isOwn;
                if (typeof factoryId != 'undefined' && !isBlank(factoryId) && !alloted){
                    if (isOwn){
                        // 自做工序，添加至外协商工序列表
                        var craftsRow = {
                            orderItemId: craftsAllotPageVm.itemId,
                            partId: craft.partId,
                            partsName: craft.partsName,
                            supplierId: factoryId,
                            craftValueType: craft.craftValueType,
                            remark: craft.remark,
                            isOwn: craft.isOwn
                        }
                        serverCraftsList.push(craftsRow);
                    }else {
                        var allotCraft = {
                            orderItemId: craftsAllotPageVm.itemId,
                            partId: craft.partId,
                            partsName: craft.partsName,
                            sellerId: factoryId,
                            craftValueType: craft.craftValueType,
                            productId: craftsAllotPageVm.productId,
                            splitProcessType: craft.splitProcessType,
                            totalPrice: craft.totalPrice,
                            self: craft.self,
                            remark: craft.remark
                        }
                        allotCrafts.push(allotCraft);
                    }
                }
            }

            var postData = {
                orderNo: craftsAllotPageVm.orderNo,
                erpItemId: craftsAllotPageVm.erpItemId,
                itemId: craftsAllotPageVm.itemId,
                csId: vm.erpOrder.csId,
                pmcId: vm.erpOrder.pmcId,
                orderCraftQuoteList: allotCrafts,
                orderCraftServerList: serverCraftsList
            };
            $.ajax({
                type: "POST",
                url: baseURL + "order/erporder/allotOrderCrafts",
                contentType: "application/json",
                data: JSON.stringify(postData),
                success: function(r){
                    if(r.code == 0){
                        craftsAllotPageVm.craftsAllotPageVisible = false;
                        vm.getInfo(craftsAllotPageVm.orderNo);
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
    }
})